package cn.jobstart.service.impl;


import cn.jobstart.common.SkList;
import cn.jobstart.common.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import cn.jobstart.modelExt.GroupRightsExt;
import cn.jobstart.dao.IGroupRightsDao;
import cn.jobstart.searchExt.GroupRightsSearchExt;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.service.IGroupRightsService;
import lombok.extern.slf4j.Slf4j;

import cn.jobstart.service.IAdminPermissionGroupService;
import cn.jobstart.pubInter.exception.SkException;
import java.util.List;
import java.util.stream.Collectors;


/**
*    @author sunke
*    @Date 2023-09-06 15:00:20
*    @description GroupRightsService      权限组权限关联
*/
@Service(value = "groupRightsService")
@Slf4j
public class GroupRightsServiceImpl extends AbstractBaseService<GroupRightsExt, GroupRightsSearchExt,String> implements IGroupRightsService {

    @Resource(name = "groupRightsDao")
    private IGroupRightsDao groupRightsDao;

    @Resource
    private IAdminPermissionGroupService adminPermissionGroupService;

    @Override
    public IBaseDao<GroupRightsExt, GroupRightsSearchExt,String> getDao() {
        return groupRightsDao;
    }


    /**
     * 添加权限组和权限关系
     *
     * @param groupId
     * @param rightsIds
     * @param delOldFlag 是否删除老的关系数据
     * @return
     */
    @Override
    public List<GroupRightsExt> insertByGroupIdAndRightsIds(String groupId, List<String> rightsIds, boolean delOldFlag) {

        try {
            if (StringUtil.isNullOrEmpty(groupId) || StringUtil.isNullOrEmpty(rightsIds)) {
                return null;
            }

            //删除老的关系数据
            if (delOldFlag) {
                batchDeleteBySearch(GroupRightsSearchExt.getInstance().setGroupIdEq(groupId));

            }

            List<GroupRightsExt> groupRightsList = SkList.getInstance();

            rightsIds.stream().forEach(rightsId -> {
                GroupRightsExt groupRights = new GroupRightsExt();
                groupRights.setGroupId(groupId);
                groupRights.setRightsId(rightsId);
                groupRightsList.add(groupRights);

            });


            return batchInsert(groupRightsList);
        } catch (SkException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    @Override
    public void clearByGroupId(String groupId) {
        if (StringUtil.isNullOrEmpty(groupId)) {
            return;
        }

        try {
            batchDeleteBySearch(GroupRightsSearchExt.getInstance().setGroupIdEq(groupId));
        } catch (SkException e) {

        }


    }

    @Override
    public List<GroupRightsExt> getListByGroupId(String groupId) {

        if (StringUtil.isNullOrEmpty(groupId)) {
            return null;
        }

        try {
            return getAllList(GroupRightsSearchExt.getInstance().setGroupIdEq(groupId));
        } catch (SkException e) {
            return null;
        }
    }

    @Override
    public List<String> getRightsIdsByStaffId(String staffId) {
        if (StringUtil.isNullOrEmpty(staffId)) {
            return null;
        }

        List<String> groupIds = adminPermissionGroupService.getGroupIdsByAdminId(staffId);
        if (StringUtil.isNullOrEmpty(groupIds)) {
            return null;
        }

        GroupRightsSearchExt search = GroupRightsSearchExt.getInstance();
        search.setGroupIds(groupIds);

        try {
            List<GroupRightsExt> groupRightsList = getAllList(search);
            if (StringUtil.isNullOrEmpty(groupRightsList)) {

                return null;
            }
            return groupRightsList.stream().map(GroupRightsExt::getRightsId).distinct().collect(Collectors.toList());


        } catch (SkException e) {
            return null;
        }
    }
}